import Vue from "vue";
import { checkType } from "@/npmPackage/components/util/index.js";
import { cloneDeep } from "lodash";
const vm = new Vue({
  name: "event_bus",
  data() {
    return {
      $_optionPath: {}, // 存 computed 初始化的值
      $_option: {} // 存初始值得
    };
  },
  methods: {
    restore() {
      this.$_optionPath = {};
      this.$_option = {};
    }
  }
});

window.event_bus = vm;

function getDefaultComputed(obj) {
  const keys = [
    {
      key: "key",
      defalultValue: "defalultKey"
    },
    {
      key: "default",
      defalultValue: ""
    },
    {
      key: "build_label",
      defalultValue: obj.key || "defalultLabel"
    },
    {
      key: "build_component",
      defalultValue: "labelInput"
    },
    {
      key: "build_option",
      defalultValue: {}
    }
  ];
  keys.forEach(keyObj => {
    obj[keyObj.key] =
      obj[keyObj.key] ||
      (checkType(keyObj.defalultValue) == "function"
        ? keyObj.defalultValue()
        : keyObj.defalultValue);
  });
  return obj;
}

function isUndefined(item) {
  if (typeof item !== undefined) {
    return item;
  } else {
    return false;
  }
}

function getPathWithRecursion(vm) {
  const pathArr = [];

  function getPathWithParrent(vm) {
    const currentPath =
      (vm.$vnode &&
        vm.$vnode.key &&
        `${vm.$options._componentTag}_${vm.$vnode.key}`) ||
      vm.$options._componentTag;
    pathArr.push(currentPath);
    if (!vm.$_isPathContain && currentPath) {
      getPathWithParrent(vm.$parent);
    } else {
      pathArr.push("root");
    }
  }
  getPathWithParrent(vm);
  return pathArr.reverse().join(".");
}
export function mapPathToComputed(
  arr = [
    {
      key: "title",
      default: "",
      build_label: "",
      build_component: "",
      build_option: ""
    }
  ]
) {
  const res = {};
  let pathCommon = "";
  // compute需要取值.
  let isGet = false;
  arr.forEach(obj => {
    res[obj.key] = function() {
      pathCommon = getPathWithRecursion(this);
      let formateObj = getDefaultComputed(obj);
      const path = `${pathCommon}.${formateObj.key}`;
      vm._data.$_optionPath[path] = {
        ...formateObj,
        key: path
      };
      if (!isGet) {
        isGet = true;
        arr.forEach(item => {
          const { key } = item;
          // 由于computed是惰性的，所以需要取一次值，否则对于在click事件中的东西，将会无法被构建器表单接收
          this[key];
        });
      }
      let value = vm._data.$_option[path];
      // const regRes = /^root\.(.+)/.exec(path)
      // if (regRes[1]) {
      //   set(vm._data.$_option, regRes[1], value)
      // }

      const res =
        isUndefined(value) || isUndefined(formateObj.default) || `${path}`;
      return res;
    };
  });
  return res;
}
export const pathContain = {
  created() {
    this.$_isPathContain = true;
  },
  watch: {
    options: {
      handler() {
        if (this.options) {
          vm._data.$_option = {
            ...vm._data.$_option,
            ...this.options
          };
        }
      },
      deep: true,
      immediate: true
    }
  },
  props: ["options"],
  mounted() {
    setTimeout(() => {
      const option = this.$_getAllOption();
      //const res = JSON.stringify(this.$_getAllOption(), null, 2);
      this.$emit("onGetBuilderOption", {
        option,
        changeValue: vm.changeValue
      });
      //  console.log(res);
    }, 500);
  },

  methods: {
    $_getAllPath() {
      const res = [];
      const origin = vm._data.$_optionPath;
      for (let key in origin) {
        res.push(origin[key]);
      }
      return res;
    },
    $_getAllOption() {
      const origin = vm._data.$_optionPath;
      const res = [];
      for (let key in origin) {
        const value = origin[key];
        res.push(value);
      }
      res.sort((a, b) => {
        return a > b ? -1 : 1;
      });
      return res;
    }
  }
};
